<?php 
   require_once 'config.php';
   $action = getValue('action','str','POST','');
   if($action == 'add'){
      $new_title = getValue('new_title','str','POST','');
      $new_content = getValue('new_content','str','POST','');
      $new_date = getValue('new_date','str','POST','');
      if($new_date == ''){
         $new_date = time();   
      }else{
         $new_date = strtotime(str_replace('/','-',$new_date));
      }
      $new_active = getValue('new_active','int','POST');
      $new_image = '';
      if(Form::isValid("myForm")){
         if(isset($_FILES['image_field'])){
            $handle = new upload($_FILES['image_field'],'vn_VN');
            if ($handle->uploaded) {
               $handle->allowed = array('image/*');
               $handle->file_max_size = 2*1024*1024;
               $handle->file_new_name_body   = dechex(rand(100,999)).Str::IDEncode(time());
               $handle->image_resize         = true;
               $handle->image_x              = 300;
               $handle->image_ratio_y        = true;
               $handle->process(PATH_IMG_UPLOAD);
               $new_image = $handle->file_dst_name;
               if ($handle->processed) {
                   $handle->clean();
               } else {
                  $error_upload = Str::alert($handle->error,'alert-error');
               }
           }
         }
         $sql = "INSERT INTO
                     news(
                        new_title,
                        new_date,
                        new_image,
                        new_content,
                        new_active
                        )
                     VALUES(
                        :title,
                        :date,
                        :image,
                        :content,
                        :active)";
         $value = array('title' => $new_title,
                         'date' => $new_date,
                         'image' => $new_image,
                         'content' => $new_content,
                         'active' => $new_active
                         );
         $db = new DB();
         $insert = $db->query($sql,$value);
         if($insert) $alert = Str::alert("Thêm mới thành công");
      }
      else{
         Form::setError("myForm","Điền đầy đủ thông tin cho sự kiện");
      }
   }
?>
<!DOCTYPE HTML>
<head>
	<meta http-equiv="content-type" content="text/html" />
	<meta name="author" content="Ta Van Chinh" />
   <?php echo $css_module ?>
   <?php echo $js_module ?>
	<title>Thêm mới sản phẩm</title>
</head>

   <body>
      <div class="head">
         <div class="isw-grid"></div>
         <h1>Thêm mới sản phẩm</h1>      
         <div class="reload_tab" title="Reload"></div>                       
         <div class="clear"></div>
      </div>
   <div class="block-fluid">
   <?php echo isset($alert) ? $alert : ""?>
   <?php 
      $form = new Form("myForm");
      $form->addElement(new Element_Hidden('action','add'));
      $form->configure(array(
      	"prevent" => array("bootstrap", "jQuery")
      ));
      $form->addElement(new Element_HTML('<div class="requie">Những dòng đánh dấu * là bắt buộc nhập</div>'));
      $form->addElement(new Element_Textbox('Tiêu đề:','new_title',array(
                        'id' => 'new_title',
                        'class' => 'span5',
                        'required' => 1,
                        'placeholder' => 'Tiêu đề tin tức')
                        ));
      $form->addElement(new Element_File('Ảnh 1(<2MB):','image_field',array(
                        'id' => 'image_field',
                        'accept' =>"image/*"
                        )));
      $form->addElement(new Element_Textbox('Thời gian:','new_date',array(
                        'id' => 'cal_date',
                        'class' => 'datepicker',
                        'placeholder' => 'Thời gian')
                        ));
      $form->addElement(new Element_Textarea('Nội dung:','new_content',array(
                        'id' => 'new_content',
                        'placeholder' => 'Nội dung')
                        ));
      $form->addElement(new Element_Checkbox('Hiển thị:','new_active',array(1 => ''),array(
                        'id' => 'new_active',
                        'value' => 1,
                        )));
      $options_chk = array(1 => '');
      $form->addElement(new Element_Button("Thêm mới","submit", array(
         'name' => 'submit',
         'id' => 'submit'
      )));
      $form->addElement(new Element_Button("Hủy", "button", array(
          "onclick" => "history.go(-1);"
      )));
      $form->render();
   ?>
</div>
<script>
   $(document).ready(function(){
      CKEDITOR.replace( 'new_content',{
          enterMode:CKEDITOR.ENTER_BR,
      });  
   });
   
</script>
</body>
</html>
